/*******************************************************************************
 * FilePath    : /ble_app_peripheral_rfTest/Src/BSP/SPI.c
 * Version     : 1.0
 * Brief       :
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-04-03 15:06:14
 * LastEditors : AndrewHu
 * LastEditTime: 2022-07-14 15:10:18
 * Description :
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
SPI_HandleTypeDef SPIHandleTypeDef = {0};
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/

/*******************************************************************************
 * Function    : SPI_CS_Enable_
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:50:24
 * Description :
 ******************************************************************************/
void SPI_CS_Enable_(void)
{
    HAL_GPIO_WritePin(SPI_NSS_GPIO, SPI_NSS_PIN, GPIO_PIN_RESET);
}
/*******************************************************************************
 * Function    : SPI_CS_Disable_
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:50:31
 * Description :
 ******************************************************************************/
void SPI_CS_Disable_(void)
{
    HAL_GPIO_WritePin(SPI_NSS_GPIO, SPI_NSS_PIN, GPIO_PIN_SET);
}
/*******************************************************************************
 * Function    : HAL_SPI_MspInit
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-14 22:00:54
 * Description :
 ******************************************************************************/
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
    __HAL_RCC_GPIOD_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_SPI_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStructure;
    /* Configure the GPIO_NSS pin */
    GPIO_InitStructure.Pin = SPI_NSS_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT;
    GPIO_InitStructure.OpenDrain = GPIO_PUSHPULL;
    GPIO_InitStructure.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStructure.SlewRate = GPIO_SLEW_RATE_LOW;
    GPIO_InitStructure.DrvStrength = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(SPI_NSS_GPIO, &GPIO_InitStructure);

    SPI_CS_Disable_();

    /* Configure the MOSI pin */
    GPIO_InitStructure.Pin = SPI_MOSI_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_AF;
    GPIO_InitStructure.Alternate = GPIO_AF3_SPI_MOSI;
    GPIO_InitStructure.OpenDrain = GPIO_PUSHPULL;
    GPIO_InitStructure.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStructure.SlewRate = GPIO_SLEW_RATE_HIGH;
    GPIO_InitStructure.DrvStrength = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStructure.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(SPI_MOSI_GPIO, &GPIO_InitStructure);
    HAL_GPIO_WritePin(SPI_MOSI_GPIO, SPI_MOSI_PIN, GPIO_PIN_SET);
    /* Configure the MISO pin */
    GPIO_InitStructure.Pin = SPI_MISO_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_AF;
    GPIO_InitStructure.Alternate = GPIO_AF3_SPI_MISO;
    GPIO_InitStructure.OpenDrain = GPIO_PUSHPULL; // GPIO_OPENDRAIN
    GPIO_InitStructure.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStructure.SlewRate = GPIO_SLEW_RATE_HIGH; // GPIO_SLEW_RATE_LOW;
    GPIO_InitStructure.DrvStrength = GPIO_DRV_STRENGTH_LOW;
    GPIO_InitStructure.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(SPI_MISO_GPIO, &GPIO_InitStructure);
    // HAL_GPIO_WritePin(SPI_MOSI_GPIO, SPI_MOSI_PIN, SET);
    /* Configure the CLK pin */
    GPIO_InitStructure.Pin = SPI_CLK_PIN;
    GPIO_InitStructure.Mode = GPIO_MODE_AF;
    GPIO_InitStructure.Alternate = GPIO_AF3_SPI_CLK;
    GPIO_InitStructure.OpenDrain = GPIO_PUSHPULL; // GPIO_OPENDRAIN;//GPIO_PUSHPULL;
    GPIO_InitStructure.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStructure.SlewRate = GPIO_SLEW_RATE_HIGH; // GPIO_SLEW_RATE_LOW;
    GPIO_InitStructure.DrvStrength = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStructure.Pull = GPIO_PULLDOWN; // GPIO_NOPULL;

    HAL_GPIO_Init(SPI_CLK_GPIO, &GPIO_InitStructure);
}
/*******************************************************************************
 * Function    : SPIM_Init
 * Brief       : SPI
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:50:40
 * Description :
 ******************************************************************************/
void SPIM_Init(void)
{
    SPIHandleTypeDef.Instance = SPI;
    SPIHandleTypeDef.Init.NSS = SPI_NSS_MODE_HIGH;
    SPIHandleTypeDef.Init.Mode = SPI_MODE_MASTER;
    SPIHandleTypeDef.Init.CLKPhase = SPI_POLARITY_LOW;
    SPIHandleTypeDef.Init.CLKPolarity = SPI_PHASE_1EDGE;
    SPIHandleTypeDef.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
    HAL_SPI_Init(&SPIHandleTypeDef);
}
/*******************************************************************************
 * Function    : SPI_WriteRead
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:52:13
 * Description :
 ******************************************************************************/
unsigned char SPI_WriteRead(unsigned char SendData, unsigned char WriteFlag)
{
    SPIHandleTypeDef.Instance->DATA = SendData;
    while (0 == (SPIHandleTypeDef.Instance->SR & SPI_SR_SPIF))
        ;
    return SPIHandleTypeDef.Instance->DATA;
}
/*******************************************************************************
 * Function    : SPI_WriteBuf
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:52:22
 * Description :
 ******************************************************************************/
unsigned char Temp, Tab[33];
unsigned char SPI_WriteBuf(unsigned char reg, unsigned char const *pBuf, unsigned char len)
{
    SPI_CS_Enable_();
    Tab[0] = reg;
    memcpy(Tab + 1, pBuf, len);
    LL_SPI_Master_TransmitReceive(&SPIHandleTypeDef, Tab, len + 1, NULL, 0);
    SPI_CS_Disable_();
    return 0;
}
/*******************************************************************************
 * Function    : SPI_ReadBuf
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:52:33
 * Description :
 ******************************************************************************/
unsigned char SPI_ReadBuf(unsigned char reg, unsigned char *pBuf, unsigned char len)
{
    SPI_CS_Enable_();
    Temp = reg;
    LL_SPI_Master_TransmitReceive(&SPIHandleTypeDef, &Temp, 1, Tab, len);
    memcpy(pBuf, Tab, len);
    SPI_CS_Disable_();
    return 0;
}
/*******************************************************************************
 * Function    : IsIrqEnabled
 * Brief       :
 * Parameter   :
 * Returns     :
 * Date        : 2020-04-03 15:52:37
 * Description :
 ******************************************************************************/
char IsIrqEnabled(void)
{
    return (!HAL_GPIO_ReadPin(IRQ_GPIO, IRQ_PIN));
}
